var module = angular.module("indexApp", []);
module.controller("indexController", function($scope, $http, $window) {
	//loading框方法，使用时将其返回jc对象，当执行完毕对其执行jc.close()方法关闭
	$scope.loading=function(){
		var content='<div class="progress active">'+
		'<div class="progress-bar progress-bar-primary progress-bar-striped" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100" style="width: 100%">'
		'</div></div>';
		var jc=$.alert({
		    title: '加载中...',
		    content: content,
		    closeIcon: false,
		    confirmButton:false
		});
		return jc;
	}
	var jc=$scope.loading();
	$http({
		method : 'post',
		url : context + '/area/findArea'
	}).success(function(data, status, headers, config) {
		$scope.areas = data.data;
		$scope.imgPath = imgPath;
		$scope.navbg = $scope.areas[0].template.winColor;
		$scope.menubg = $scope.areas[0].template.navColor;
		$scope.fontcolor = $scope.areas[0].template.fontColor;
		$scope.buttoncolor = $scope.areas[0].template.buttonColor;
		$scope.fonfamily = $scope.areas[0].template.fontFamily;
		jc.close();
	});

	// 改变窗体颜色代码
	angular.element($window).bind("scroll", function(e) {
		var top = $(document).scrollTop(); // 当前页面所在的scrollTop
		for ( var i = 0; i < $scope.areas.length; i++) {
			var areaId = $scope.areas[i].areaId;
			var areaTop = $("#" + areaId).offset().top; // 不同模块的offsetTop
			if (top <= areaTop) {
				// 刷新背景图片颜色等
				$scope.navbg = $scope.areas[i].template.winColor;
				$scope.menubg = $scope.areas[i].template.navColor;
				$scope.fontcolor = $scope.areas[i].template.fontColor;
				$scope.buttoncolor = $scope.areas[i].template.buttonColor;
				$scope.fonfamily = $scope.areas[i].template.fontFamily;
				$scope.$apply();
				break;
			}

		}
	});

	$scope.isAdmin = function() {

		$http({
			method : 'post',
			url : context + '/user/isAdmin'
		}).success(function(data, status, headers, config) {

			if (data.admin == true) {
				location.href = context + "/user/userList";
			} else {
				$.alert({
					title : '您不是管理员，没有权限!',
					cancelButton : false,
					content : false,
					confirmButton : '确认',
					theme : 'white'
				});
			}
		});
	}

});
module.directive("nicescroll", function() {
	return {
		restrict : "A",
		link : function($scope, ele, attr, ctrl) {
			ele.mCustomScrollbar({
				theme : "minimal-dark",
				callbacks : {
					onTotalScroll : function() {
						$scope.pushContent();
					},
					onTotalScrollOffset : 0,
					whileScrolling : false,
					whileScrollingInterval : 10
				}
			});
		}
	}
});

addEventListener("DOMContentLoaded", function() {
	var body = document.body, html = document.documentElement;
	var itv, height = document.body.offsetHeight;
	var page = scrollTop() / height | 0;
	// 窗口大小改变事件
	addEventListener("resize", onresize), onresize();
	// 滚轮事件
	document.body.addEventListener("onwheel" in document ? "wheel"
			: "mousewheel", function(e) {
		clearTimeout(itv);
		itv = setTimeout(function() {
			var delta = e.wheelDelta / 120 || -e.deltaY / 3;
			page = scrollTop() / height | 0;
			page -= delta;
			var max = (document.body.scrollHeight / height | 0) - 1;
			if (page < 0)
				return page = 0;
			if (page > max)
				return page = max;
			move();
		}, 100);
		e.preventDefault();
	});
	// 平滑滚动
	function move() {
		var value = height * page;
		var diff = scrollTop() - value;
		(function callee() {
			diff = diff / 1.2 | 0;
			scrollTop(value + diff);
			if (diff)
				itv = setTimeout(callee, 16);
		})();
	}
	;
	// resize事件
	function onresize() {
		height = body.offsetHeight;
		move();
	}
	;
	// 获取或设置scrollTop
	function scrollTop(v) {
		if (v == null)
			return Math.max(body.scrollTop, html.scrollTop);
		else
			body.scrollTop = html.scrollTop = v;
	}
	;
});
module.directive('hover',[function(){
    return {
        restrict:'A',
        link:function(scope,ele,attrs){
            ele.bind('mouseover',function(){
               ele.toggleClass('active');
            });
            ele.bind('mouseout',function(){
                ele.toggleClass('active');
            });
        }
    }
}]);